﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TunnelBackground
{
    public class AlarmAnalysis
    {
        public string tempinfo = "\r\n手报  ::  11:12:31 05/04/2018 控制盘:01  卡:03  器件:0129\r\n灌林岩山隧道 手报07号\r\n";

        private string alarmString;
        private string alarmType;
        private DateTime alarmTime;
        private string controlPanel;
        private string card;
        private string device;

        private string[] alarmStringSplited;
        /// <summary>
        /// 分析之前的字符串
        /// </summary>
        public string AlarmString
        {
            get
            {
                return alarmString;
            }

            set
            {
                alarmString = value;
                Analysis();
            }
        }
        /// <summary>
        /// 报警类型
        /// </summary>
        public string AlarmType
        {
            get
            {
                return alarmType;
            }

            set
            {
                alarmType = value;
            }
        }
        /// <summary>
        /// 报警时间
        /// </summary>
        public DateTime AlarmTime
        {
            get
            {
                return alarmTime;
            }

            set
            {
                alarmTime = value;
            }
        }
        /// <summary>
        /// 控制盘号码
        /// </summary>
        public string ControlPanel
        {
            get
            {
                return controlPanel;
            }

            set
            {
                controlPanel = value;
            }
        }
        /// <summary>
        /// 卡号
        /// </summary>
        public string Card
        {
            get
            {
                return card;
            }

            set
            {
                card = value;
            }
        }
        /// <summary>
        /// 设备号
        /// </summary>
        public string Device
        {
            get
            {
                return device;
            }

            set
            {
                device = value;
            }
        }

        /// <summary>
        /// 对单独一条报警信息进行分析
        /// </summary>
        /// <param name="AlarmString">报警信息</param>
        public AlarmAnalysis(string AlarmString)
        {
            this.AlarmString = AlarmString;
        }

        public void Analysis()
        {
            try
            {
                
                string[] preresult = Regex.Split(AlarmString, "\r\n", RegexOptions.IgnoreCase);
                string[] result = Regex.Split(preresult[1], " ", RegexOptions.IgnoreCase);
                alarmStringSplited = result;
                AlarmType = result[0];
                AlarmTime = DateTime.ParseExact(result[4]+" "+result[5], "HH:mm:ss MM/dd/yyyy", System.Globalization.CultureInfo.CurrentCulture);
                ControlPanel = result[6].Split(':')[1];
                Card = result[8].Split(':')[1];
                Device = result[10].Split(':')[1];
            }
            catch
            {
                MessageBox.Show(AlarmString);
                AlarmType = "错误";
                AlarmTime = DateTime.ParseExact("00:00:00 01/01/1900", "HH:mm:ss MM/dd/yyyy", System.Globalization.CultureInfo.CurrentCulture);
                ControlPanel = "00";
                Card = "00";
                Device = "0000";
            }
        }

        
        #region 获得各项单独参数，意义不大，调试用
        public string getAlarmType()
        {
            return alarmStringSplited[0];
        }

        public DateTime getAlarmTime()
        {
            DateTime dt = DateTime.ParseExact(alarmStringSplited[2], "HH:mm:ss MM/dd/yyyy", System.Globalization.CultureInfo.CurrentCulture);
            return dt;
        }

        public string getControlPanel()
        {
            string[] cp = alarmStringSplited[3].Split(':');
            return cp[1];
        }
        public string getCard()
        {
            string[] cp = alarmStringSplited[4].Split(':');
            return cp[1];
        }
        public string getDevice()
        {
            string[] cp = alarmStringSplited[5].Split(':');
            return cp[1];
        }
        #endregion
    }
}
